<template>
  <div class="clue-management-container">
    <div class="search-area">
      <div class="search-inputs">
        <el-input
          v-model="searchParams.responsibleCadreid"
          placeholder="请输入负责干部编号"
          clearable
          style="width: 200px; margin-right: 10px;"
        />
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </div>
    </div>
    <!-- 表格区域 -->
    <el-table :data="pagedClues" border stripe style="width: 100%" class="clue-table">
      <el-table-column prop="clueId" label="线索编号" width="120" align="center" />
      <el-table-column prop="belongArea" label="所属辖区" min-width="180" />
      <el-table-column label="风险类型" min-width="200">
        <template #default="{ row }">
          {{ row.riskType || '-' }}  <!-- 显示字符串，空值时显示 - -->
        </template>
      </el-table-column>
      <el-table-column label="风险级别" width="120" align="center">
        <template #default="{ row }">
          <el-tag :type="getLevelTagType( row.riskLevel)">
            {{ row.riskLevel }}级
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="riskRemark" label="风险备注" width="120" align="center">
        <template #default="{ row }">
          {{ row.riskRemark || '无' }}
        </template>
      </el-table-column>
      <el-table-column prop="helpGuidance" label="帮扶指导" min-width="180" />
      <el-table-column prop="responsibleCadreid" label="负责干部编号" min-width="180" />
      <el-table-column prop="currentStage" label="目前状态" width="120" align="center">
        <template #default="{ row }">
          {{ row.currentStage|| '无' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="handleEdit(row)">
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <div class="pagination-container">
      <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :total="totalClues"
        :page-sizes="[5, 10, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>

    <!-- 修改帮扶指导的对话框 -->
    <el-dialog v-model="editDialogVisible" title="修改帮扶指导" width="500px" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="100px">
        <el-form-item label="线索编号:">
          <span>{{ editForm.clueId }}</span>
        </el-form-item>
        <el-form-item label="所属辖区:">
          <span>{{ editForm.belongArea }}</span>
        </el-form-item>
        <el-form-item label="风险类型:">
          <span>{{ editForm.riskType }}</span>
        </el-form-item>
        <el-form-item label="风险级别:">
          <el-tag :type="getLevelTagType(editForm.riskLevel)">
            {{ editForm.riskLevel }}级
          </el-tag>
        </el-form-item>
        <el-form-item label="帮扶指导:" prop="helpGuidance">
          <el-input v-model="editForm.helpGuidance" type="textarea" :rows="4" placeholder="请输入帮扶指导意见" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmEdit">确认修改</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
// 6.21日需要完成表格的验证以及之后的增加帮扶指导的内容
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { getClueTables,updateClueTables } from "@/api/province"

// 分页参数
const currentPage = ref(1)
const pageSize = ref(5)
const pagedClues = ref([])
// 计算总线索数
const totalClues = ref(0)
const searchParams = reactive({
  currentPage: currentPage.value,
  pageSize: pageSize.value,
  responsibleCadreid:''
})

const queryClue = () => {
  getClueTables(searchParams).then((res) => {
    if (res.code == 2000) {
      pagedClues.value = res.data
      totalClues.value = res.total
    } else {
      pagedClues.value = []
      totalClues.value = 0
    }
  })
}
queryClue()

 const handleSearch = () => {
      queryClue()
    }

// 处理分页大小变化
const handleSizeChange = (val) => {
  searchParams.pageSize = val
  currentPage.value = 1 // 重置到第一页
  queryClue()
}


// 处理当前页变化
const handleCurrentChange = (val) => {
  searchParams.currentPage = val
  queryClue()
}

// 编辑对话框相关
const editDialogVisible = ref(false)

const editForm = reactive({
  clueId: '',
  responsibleCadreid:'',
  belongArea: '',
  riskType:'',
  riskLevel: 0,
  riskRemark: '',
  helpGuidance: '',
  isHighRisk: false
})

// 获取风险级别对应的标签类型
const getLevelTagType = (level) => {
  switch (level) {
    case 1:
      return ''
    case 2:
      return 'success'
    case 3:
      return 'warning'
    case 4:
      return 'danger'
    default:
      return 'info'
  }
}

// 点击修改按钮
const handleEdit = (row) => {
  editDialogVisible.value = true
  
  console.log(`editForm------------>`,editForm.value)
  Object.assign(editForm, JSON.parse(JSON.stringify(row)))
  
}

const updateForm= reactive({
  clueId: '',
  helpGuidance: '',
})
// 确认修改
const confirmEdit = () => {
  updateForm.clueId=editForm.clueId
  updateForm.helpGuidance=editForm.helpGuidance
  updateClueTables(updateForm).then(res => {
        if (res.code == 2000) {
          ElMessage.success('修改成功')
          editDialogVisible.value = false
          queryClue() // 重新筛选数据
        }
      })
}
</script>

<style scoped>
.clue-management-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.clue-table {
  margin-bottom: 20px;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>